% % % % % % % % % % % % % % % % % % % % % 
% 
% Alpha Beta Prunning : Gruppe G
% 
% % % % % % % % % % % % % % % % % % % % % 

function [val,b]=AlphaBetaPrunning(board,depth,currentPlayer, alpha, beta)

if(depth==0)% if depth=0 return value
    val=evalfunc( board, currentPlayer);
%     b=board;
    fprintf('Current Player: %d\n',currentPlayer)
    fprintf('DEPTH: %d\nVALUE: %d \n\n' ,depth,val);
    return
% else
%     val=NaN;
%     b=board;
end

% 
% if (64-size(find(board),1)==0)% if player can't play
%     val=evalfunc( board, currentPlayer);
%     b=board;
%     return
% end

% Get the list of plausable moves, sorted by most pieces flipped
moves = GetValidFieldsNoValues( board , currentPlayer );% weighted matrix!
[row,column,value]=find(moves);
PosMat=sortrows([row,column,value],-3);% Sortieren!
row=PosMat(:,1);
column=PosMat(:,2);
value=PosMat(:,3);
numPosMoves = length(row);

if numPosMoves == 0   % no posibble moves
    if nargout == 2     
        b = board;      
        val = 0;          
        return;
    end
    % Gegner am Zug -> Werte negieren; alpha und beta vertauschen
    val = AlphaBetaPrunning(board,depth,-currentPlayer, alpha, beta);
    return;
end
    
%  Find the best currentMove
bestMove=zeros(2,1);
%Copy the board
% currentBoard=board;
fprintf('\n\tCurrent Player: %d\n',currentPlayer)
fprintf('\tDEPTH: %d\n' ,depth);
display(board)
fprintf('\tMoegliche Zuege: %d\n' ,length(value));
for CurrentMoveIdx=1:numPosMoves
    fprintf('TIEFE: %d Current Index: %d\n',depth,CurrentMoveIdx)
        %       Copy the board
%         currentBoard=currentBoard;
        %       Make the selected move
%         currentBoard=currentBoard2;
         currentBoard=make_move(board,row(CurrentMoveIdx),column(CurrentMoveIdx),currentPlayer);
        %       Evaluate the board
%         if(depth-1==0)
             currentValue= AlphaBetaPrunning(currentBoard, depth-1, -currentPlayer, alpha, beta);
%         else
%              [currentValue,currentBoard]= AlphaBetaPrunning(currentBoard, depth-1, -currentPlayer, alpha, beta);         
%         end
        
        
%         currentValue= AlphaBetaPrunning(currentBoard, depth-1, -1*currentPlayer, alpha, beta);
        %       MAX
        if(currentPlayer==1)
             if (currentValue > alpha)
                
                alpha = currentValue;
                bestMove = [row(CurrentMoveIdx); column(CurrentMoveIdx)];
             end
%             // Prune?
            if (alpha >= beta)                            
                val= alpha;

                return
%                 break
            end                   
        else
        %      MIN
            if (currentValue < beta)                    
                beta = currentValue;
                bestMove = [row(CurrentMoveIdx); column(CurrentMoveIdx)];
            end
%                     // Prune?
            if (alpha >= beta)

                val= beta;
                return
%                 break
            end     
        end       
end

%  Make the best move  and return new board 
if nargout == 2 
%     if ((bestMove(1)==0)&&(bestMove(2)==0))
%         b=board;
%     else
        b=make_move(board,bestMove(1),bestMove(2),currentPlayer);
%     end  
    display(b)
end

if (currentPlayer==1)
    val=alpha;
else
    val=beta;
end

fprintf('\nCurrent Player: %d\n',currentPlayer)
fprintf('DEPTH: %d\n' ,depth);
if (currentPlayer==1)
    display(['MEIN NÄCHSTER ZUG ROW: ',num2str(bestMove(1)),' COLUMN: ',num2str(bestMove(2))])
else
    display(['GEGNER ZUG ROW: ',num2str(bestMove(1)),' COLUMN: ',num2str(bestMove(2))])
end

display(val)


